package com.cy.pj.modules.controller;

import com.cy.pj.module.pojo.RequestParameter;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 在此对象中演示请求中参数的处理过程
 */

@RestController
public class ParameterObjectController {
    @RequestMapping("/doParam01")
    //http://localhost/doParam01?id=1
    public String doMethodParam(HttpServletRequest request){
        String idStr= request.getParameter("id");
        Integer id= Integer.parseInt(idStr);
        return "request params id's value is"+id;
    }
    @RequestMapping("/doParam02")
   // http://localhost/doParam02?id=5
    public String doMethodParam(Integer id){
        return "request params id's value is"+id;
    }
    @RequestMapping("/doParam03")
    //http://localhost/doParam03?ids=5,4,3,2,1
    public String doMethodParam(Integer[] ids){
        return "request params id's value is"+ Arrays.toString(ids);
    }
    @RequestMapping("/doParam04")
    //http://localhost/doParam04?startTime=2020/5/21
    public String doMethodParam(@DateTimeFormat(pattern = "yyyy/MM/dd")Date startTime){
        return "request params id's value is"+startTime;
    }
    @RequestMapping("/doParam05")
    //http://localhost/doParam05?user name=tony&page=5
    public String doMethodParam(@RequestParam("username") String name,
                                @RequestParam(required = true) Integer page){
        return "request params id's value is"+name+":"+page;
    }
    //=============================POJO对象方法===========================
    @GetMapping("/doParam06")
    //http://localhost/doParam06?name=tony
    public String doMethodParam(RequestParameter parameter){
        return "request params id's value is"+ parameter.toString();
    }
   // @GetMapping("/doParam07")//http://localhost/doParam07?name=tony&ids=5,2
    @RequestMapping("/doParam07")
    public String doMethodParam(RequestParameter parameter,Integer[] ids){
        return "request params id's value is"+ parameter.toString()+",ids"+Arrays.toString(ids);
    }
    @PostMapping("/doParam08")
    public String doMethodParam(Map<String,Object> map){
        return "request params id's value is"+ map.toString();
    }
    @GetMapping("/doParam10/{id}/{name}")
    public String doMethodPRestUrlaram(@PathVariable Integer id,@PathVariable String name){
        return "request params id's value is"+id+",name"+name;
    }
}
